package com.engine.api.dckj.mh.service.impl;

import com.engine.api.dckj.mh.service.UserLocationService;
import com.engine.api.dckj.mh.entity.UserLocationInfo;
import com.wbi.util.Util;
import org.jetbrains.annotations.NotNull;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;

import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class UserLocationServiceImpl  implements UserLocationService {
    private static Logger logger = Logger.getLogger(UserLocationServiceImpl.class.getName());
    @Override
    public List<UserLocationInfo> getUserLocationByUserIdList(List<String> userIds) {
        RecordSetDataSource recordSet = new RecordSetDataSource();
        if (userIds == null || userIds.isEmpty()) {
            return Collections.emptyList();
        }
        String collect = userIds.stream().filter(Objects::nonNull).distinct()
                .map(String::valueOf)
                .collect(Collectors.joining(","));
        String sql = "SELECT  l.create_user_id, l.create_time, l.longitude, l.latitude, l.locating\n" +
                "    FROM em_sys_baseuser_location l\n" +
                "    INNER JOIN (\n" +
                "        SELECT \n" +
                "            create_user_id, \n" +
                "            MAX(create_time) as max_create_time\n" +
                "        FROM em_sys_baseuser_location\n" +
                "        WHERE 1=1\n" +
                "                AND create_user_id IN("+collect+")\n" +
                "               \n" +
                "        GROUP BY create_user_id\n" +
                "    ) latest ON l.create_user_id = latest.create_user_id \n" +
                "             AND l.create_time = latest.max_create_time\n" +
                "    ORDER BY l.create_time asc";
        recordSet.executeQueryWithDatasource(sql,"em7");
        List<UserLocationInfo> infoList  = new ArrayList<>();
        while (recordSet.next()){
            UserLocationInfo userLocationInfo = getDataSourceUserLocationInfo(recordSet);
            infoList.add(userLocationInfo);
        }
        for (UserLocationInfo userLocationInfo : infoList) {
            RecordSet recordSet1 = new RecordSet();
            String createUserId = userLocationInfo.getCreate_user_id();
            String sql1 = "select lastname from hrmresource where id = " + createUserId+" and status < 3";
            recordSet1.executeQuery(sql1);
            while (recordSet1.next()) {
                String lastname = Util.null2String(recordSet1.getString("lastname"));
                userLocationInfo.setUser_name(lastname);
            }
        }
        return infoList;
    }

    @Override
    public List<UserLocationInfo> getUserLocationByUserAndDateRange(String id, String startTime, String endTime) {
        RecordSetDataSource recordSet = new RecordSetDataSource();
        if(id !=null && startTime !=null && endTime !=null) {
            String sql = "SELECT create_user_id,create_time,longitude,latitude,locating FROM em_sys_baseuser_location   where create_user_id = "+id+" and create_time> '"+startTime+"' and create_time< '"+endTime+"' order by create_time asc";
            logger.info("全部不传执行查询sql："+ sql);
            recordSet.executeQueryWithDatasource(sql,"em7");
            List<UserLocationInfo> infoList = new ArrayList<>();
            while (recordSet.next()) {
                UserLocationInfo userLocationInfo = getDataSourceUserLocationInfo(recordSet);
                infoList.add(userLocationInfo);
            }
            for (UserLocationInfo userLocationInfo : infoList) {
                RecordSet recordSet1 = new RecordSet();
                String createUserId = userLocationInfo.getCreate_user_id();
                String sql1 = "select lastname from hrmresource where id = " + createUserId+" and status < 3";
                recordSet1.executeQuery(sql1);
                while (recordSet1.next()) {
                    String lastname = Util.null2String(recordSet1.getString("lastname"));
                    userLocationInfo.setUser_name(lastname);
                }
            }
            return infoList;
        }
        if(id != null && startTime == null && endTime == null) {
            String sql ="SELECT create_user_id,create_time,longitude,latitude,locating FROM em_sys_baseuser_location   where create_user_id = "+id+"  order by create_time asc";
            logger.info("只传用户id执行查询sql："+ sql);
            return getUserLocationInfos(recordSet, sql);
        }
        if (id != null && endTime != null) {
            String sql ="SELECT create_user_id,create_time,longitude,latitude,locating FROM em_sys_baseuser_location   where create_user_id = "+id+"  and create_time< '"+endTime+"' order by create_time asc";
            logger.info("传用户id+结束时间执行查询sql："+ sql);
            return getUserLocationInfos( recordSet, sql);
        }
        if(id ==null && startTime !=null && endTime !=null) {
            String sql ="SELECT create_user_id,create_time,longitude,latitude,locating FROM em_sys_baseuser_location   where  create_time> '"+startTime+"' and create_time< '"+endTime+"' order by create_time asc";
            logger.info("传开始时间+结束时间执行查询sql："+ sql);
            return getUserLocationInfos(recordSet, sql);
        }
        if (id != null) {
            String sql ="SELECT create_user_id,create_time,longitude,latitude,locating FROM em_sys_baseuser_location   where create_user_id = "+id+" and create_time> '"+startTime+"' order by create_time asc";
            logger.info("传用户id+开始时间执行查询sql："+ sql);
            return getUserLocationInfos(recordSet, sql);
        }
        return null;
    }

    @NotNull
    private List<UserLocationInfo> getUserLocationInfos(RecordSetDataSource recordSet, String sql) {
        recordSet.executeQueryWithDatasource(sql,"em7");
        List<UserLocationInfo> infoList  = new ArrayList<>();
        while (recordSet.next()){
            UserLocationInfo userLocationInfo = getDataSourceUserLocationInfo(recordSet);
            infoList.add(userLocationInfo);
        }
        for (UserLocationInfo userLocationInfo : infoList) {
            RecordSet recordSet1 = new RecordSet();
            String createUserId = userLocationInfo.getCreate_user_id();
            String sql1 = "select lastname from hrmresource where id = " + createUserId+" and status < 3";
            recordSet1.executeQuery(sql1);
            while (recordSet1.next()) {
                String lastname = Util.null2String(recordSet1.getString("lastname"));
                userLocationInfo.setUser_name(lastname);
            }
        }
        return infoList;
    }

    @NotNull
    private static UserLocationInfo getDataSourceUserLocationInfo(RecordSetDataSource recordSet) {
        UserLocationInfo  userLocationInfo = new UserLocationInfo();
        String create_user_id = Util.null2String(recordSet.getString("create_user_id"));
        String create_time = Util.null2String(recordSet.getString("create_time"));
        String longitude = Util.null2String(recordSet.getString("longitude"));
        String latitude = Util.null2String(recordSet.getString("latitude"));
        String locating = Util.null2String(recordSet.getString("locating"));
        userLocationInfo.setCreate_user_id(create_user_id);
        userLocationInfo.setCreate_time(create_time);
        userLocationInfo.setLongitude(longitude);
        userLocationInfo.setLatitude(latitude);
        userLocationInfo.setLocating(locating);
        return userLocationInfo;
    }
}
